package me.firstapp.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串的帮助类，提供静态方法，不可以实例化。
 * 
 * @Created by keanu.
 * @Copyright (c) 2016, All Rights Reserved.
 * @website http://firstapp.me
 */
public class StrUtils {

	/**
	 * 禁止实例化
	 */
	private StrUtils() {
	}

	/**
	 * 判断字符串是否为邮件地址
	 * 
	 * @param text
	 *            字符串
	 */
	public static boolean isMail(String text) {
		// 若为空，返回false
		if (isNULL(text)) {
			return false;
		}
		Pattern p = Pattern
				.compile("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
		Matcher m = p.matcher(text);
		return m.matches();
	}

	/**
	 * 判断是否为手机号
	 * 
	 * @param mobile
	 */
	public static boolean isMobile(String mobile) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobile);
		return m.matches();
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param text
	 *            字符串
	 */
	public static boolean isNULL(String text) {
		if (text == null || text.trim().equals("") || "null".equalsIgnoreCase(text)) {
			return true;
		}
		return false;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param text
	 *            字符串
	 */
	public static boolean isNotNULL(String text) {
		return !isNULL(text);
	}

	/**
	 * 判断昵称的组成只能是字母、汉字、数字、下划线
	 * 
	 * @param name
	 */
	public static boolean isUserName(String name) {
		String str = "^[0-9a-zA-Z_\u3E00-\u9FA5]+$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(name);
		return m.matches();
	}

	/**
	 * 输入流转换字符串
	 * 
	 * @param in
	 */
	public static String getStrFromInputStream(InputStream in) {
		StringBuffer buffer = new StringBuffer();
		try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(in, "UTF-8"));
			String line = "";
			while ((line = bf.readLine()) != null) {
				buffer.append(line);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}

	public static String getLoginToken() {
		String uuidString = UUID.randomUUID().toString().replaceAll("-", "");
		return uuidString.toUpperCase();
	}

	/**
	 * 获取随机验证码
	 * 
	 * @param length
	 *            验证码长度
	 */
	public static String getVerifyCode(int length) {
		StringBuffer buffer = new StringBuffer("0123456789");
		int range = buffer.length();
		StringBuffer sb = new StringBuffer();
		Random r = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(buffer.charAt(r.nextInt(range)));
		}
		return sb.toString();
	}
	
	public static String getNum(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			char c = x(sb);
			sb.append(c);
		}
		return sort(sb.toString(), length);
	}

	private static char x(StringBuffer sb) {
		StringBuffer buffer = new StringBuffer("0123456789");
		int range = buffer.length();
		Random r = new Random();
		char c = buffer.charAt(r.nextInt(range));
		if (!sb.toString().contains(c + "")) {
			return c;
		} else {
			return x(sb);
		}
	}

	private static String sort(String str, int length) {
		int[] arr = new int[length];
		for (int i = 0; i < length; i++) {
			arr[i] = Integer.valueOf(str.charAt(i) + "").intValue();
		}
		Arrays.sort(arr);
		StringBuffer buffer = new StringBuffer();
		for (int j = 0; j < arr.length; j++) {
			buffer.append(arr[j]);
		}
		return buffer.toString();
	}

	public static void main(String[] args) {
		System.out.println(getVerifyCode(6));
	}
}
